package model;

import entity.Cell;
import observer.RobotObserver;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author minhtue001
 */
public class Robot {
    private static final Robot robot = new Robot();
    public static final int SIZE = 3;
    
    public static final int UP = 0;
    public static final int RIGHT = 1;
    public static final int DOWN = 2;
    public static final int LEFT = 3;
    
    public static final int dx[] = {0, 1, 0, -1};
    public static final int dy[] = {-1, 0 , 1, 0};
    
    private int x;
    private int y;
    private int direction;
    
    private Robot() {
        direction = RIGHT;
    }
    
    public static Robot getInstance() {
        return robot;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
        RobotObserver.getInstance().noticeView();
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getDirection() {
        return direction;
    }

    public void setDirection(int direction) {
        this.direction = direction;
    }
    
    public void rotateRight() {
        direction = (direction + 1) % 4;
        RobotObserver.getInstance().noticeView();
    }
    
    public void rotateLeft() {
        direction = (direction - 1 + 4) % 4;
        RobotObserver.getInstance().noticeView();
    }
    
    public void moveForward() {
        moveForward(1);
    }
    
    public void moveForward(int numberOfMove) {
        int newX = x + dx[direction] * numberOfMove;
        int newY = y + dy[direction] * numberOfMove;
        
        boolean check = true;
        for (int i = 0; i < SIZE; i++)
            for (int j = 0; j < SIZE; j++) {
                if (!Arena.getInstance().insideArena(new Cell(newX + i, newY +j)))
                    check = false;
            }
        
        if (check) {
            x = newX;
            y = newY;
            RobotObserver.getInstance().noticeView();
            
            for (int i= 0; i < SIZE; i++)
                for (int j = 0; j < SIZE; j++)
                    Arena.getInstance().addEmptyCell(new Cell(x + i, y + j));
                
        }
    }
}
